import fetch from '../utils/fetch';
import { message } from 'antd';
import config from '../config';
import { OPEN_LESSON } from '../actionTypes';

export const update = (payload) => ({
  type: OPEN_LESSON,
  payload,
});

export const getData = (openId) => async (dispatch) => {
  if (!openId || openId.length !== 32) {
    return false;
  }
  const detail = await fetch({
    url: `${config.HOST}/open/lesson/${openId}`,
  });

  if (detail.code === 200) {
    const data = { ...detail.data };
    try {
      dispatch(update(data));
    } catch (ex) {
      dispatch(update({ error: data.error || ex }));
    }
  } else {
    message.error('获取数据失败');
  }
};
